home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransSkel / Auxiliary / SkelApple.c next >
Encoding:
C/C++ Source or Header  |  1994-02-20  |  3.9 KB  |  142 lines  |  [TEXT/KAHL]

  1. /*
  2.  * SkelApple.c - TransSkel 3.0 Apple menu handler
  3.  */
  4.  
  5. # include    "TransSkel.h"
  6.  
  7.  
  8. /*
  9.  * Default Apple menu handler support variables
  10.  *
  11.  * appleID is set to skelAppleMenuID if SkelApple() is called and
  12.  * becomes the id of the Apple menu.
  13.  *
  14.  * appleSelect is the procedure to execute if there is a non-DA
  15.  * selection from the Apple menu.
  16.  *
  17.  * appleApplItems is true if the application installs one or more
  18.  * items of its own at the top of the Apple menu.
  19.  */
  20.  
  21. static MenuHandle    appleMenu;
  22. static short        appleID = 0;
  23. static SkelMenuSelectProcPtr appleSelect = nil;
  24. static Boolean        appleApplItems = false;
  25.  
  26.  
  27. /*
  28.  * Apple menu handler routines
  29.  *
  30.  * If the application installed its own items into the menu and the
  31.  * selection was one of them, pass the item number to the selection
  32.  * procedure.  Otherwise the item is a desk accessory. The port is
  33.  * saved and restored because it is not always preserved correctly
  34.  * across the call to OpenDeskAcc() (IM I-440).
  35.  *
  36.  * DoAppleClobber() disposes of the Apple menu and resets the other
  37.  * Apple menu handler variables.
  38.  */
  39.  
  40.  
  41. static pascal void
  42. DoAppleItem (short item)
  43. {
  44. GrafPtr    curPort;
  45. Str255    str;
  46. Handle    h;
  47. short    i;
  48.  
  49.     /*
  50.      * If there are application items, determine if item is one of them.
  51.      * Can tell by tracking backward through the items; if the gray line
  52.      * separating application items and DA's if not found before top of
  53.      * menu is reached, then its an application item, else a DA.  This
  54.      * strategy requires that there be no "-" items in the items string
  55.      * passed to SkelApple().
  56.      */
  57.     if (appleApplItems)
  58.     {
  59.         for (i = item; i > 0; i--)    /* look from current to item 1 */
  60.         {
  61.             GetItem (appleMenu, i, str);
  62.             if (str[0] == 1 && str[1] == '-')
  63.                 break;
  64.         }
  65.         if (i == 0)                    /* reached top without seeing line */
  66.         {
  67.             if (appleSelect != nil)    /* call select proc if there is one */
  68.                 (*appleSelect) (item);
  69.             return;
  70.         }
  71.     }
  72.  
  73.     /* either no application items or selection isn't one of them */
  74.     GetPort (&curPort);
  75.     GetItem (appleMenu, item, str);            /* get DA name */
  76.     SetResLoad (false);
  77.     h = GetNamedResource ('DRVR', str);
  78.     SetResLoad (true);
  79.     if (h != (Handle) nil)
  80.     {
  81.         ResrvMem (SizeResource (h) + 0x1000);
  82.         (void) OpenDeskAcc (str);            /* open it */
  83.     }
  84.     SetPort (curPort);
  85. }
  86.  
  87.  
  88. static pascal void
  89. DoAppleClobber (MenuHandle menu)
  90. {
  91.     DisposeMenu (menu);        /* menu will be == appleMenu here */
  92.     appleID = 0;
  93.     appleApplItems = false;
  94.     appleSelect = nil;
  95. }
  96.  
  97.  
  98. /*
  99.  * Install a handler for the Apple menu.
  100.  *
  101.  * SkelApple() is called if TransSkel is supposed to handle the apple
  102.  * menu itself.
  103.  *
  104.  * items contains the title of any items the application
  105.  * wants to install at the top of the menu.  If items is empty or nil, then
  106.  * only desk accessories are put into the menu.  If not empty, then the items
  107.  * are installed at the top, followed by a gray line, then the desk
  108.  * accessories.
  109.  *
  110.  * doSelect is the procedure to be called when a non-DA selection is
  111.  * made and is passed the item number.  If doSelect is nil, the selection
  112.  * is ignored.
  113.  *
  114.  * SkelApple() does not cause the menubar to be drawn, so if the Apple
  115.  * menu is the only menu, DrawMenuBar() must be called afterward.
  116.  *
  117.  * No value is returned, unlike SkelMenu().  It is assumed that
  118.  * SkelApple() will be called so early in the application that the call
  119.  * to SkelMenu() is virtually certain to succeed.  If it doesn't,
  120.  * presumably there's little hope for the application anyway.
  121.  */
  122.  
  123. pascal void
  124. SkelApple (StringPtr items, SkelMenuSelectProcPtr doSelect)
  125. {
  126.     appleID = skelAppleMenuID;
  127.     /* 024 = apple character */
  128.     appleMenu = NewMenu (appleID, (StringPtr) "\p\024");
  129.     if (items != (StringPtr) nil && items[0] > 0)
  130.     {
  131.         /* application has own items in menu */
  132.         appleApplItems = true;
  133.         /* add items */
  134.         AppendMenu (appleMenu, items);
  135.         /* add gray line */
  136.         AppendMenu (appleMenu, (StringPtr) "\p(-");
  137.         appleSelect = doSelect;
  138.     }
  139.     AddResMenu (appleMenu, 'DRVR');        /* add desk accessories */
  140.     (void) SkelMenu (appleMenu, DoAppleItem, DoAppleClobber, false, false);
  141. }
  142.